package com.langyiquan.front.controller;

import com.langyiquan.bo.ShopcartBO;
import com.langyiquan.exception.http.ParamException;
import com.langyiquan.service.ItemService;
import com.langyiquan.utils.JsonUtils;
import com.langyiquan.utils.RedisOperator;
import com.langyiquan.utils.UnifyResponse;
import com.langyiquan.vo.ShopcartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(value = "购物车", tags = {"购物车相关接口"})
@RestController
@RequestMapping("shopcart")
public class ShopCartController extends BaseController {

    @Autowired
    private RedisOperator redisOperator;

    @Autowired
    private ItemService itemService;


    @ApiOperation(value = "清空购物车商品",notes = "清空购物车商品",httpMethod = "GET")
    @GetMapping("clear")
    public UnifyResponse clear( HttpServletRequest request){
        String userId = getPassportUserId(request);
        redisOperator.del(SHOP_CART + ":" +userId);
        return UnifyResponse.success();
    }

    @ApiOperation(value = "获取购物车商品",notes = "获取购物车商品",httpMethod = "GET")
    @GetMapping("get")
    public UnifyResponse get( HttpServletRequest request){
        String userId = getPassportUserId(request);
        String shopCartJson = redisOperator.get(SHOP_CART + ":" +userId);
        List<ShopcartBO> shopcartList = null;
        List<ShopcartVO> shopcartVOS = null;
        if(StringUtils.isNotBlank(shopCartJson)){
            shopcartList = JsonUtils.jsonToList(shopCartJson,ShopcartBO.class);
            List ids = new ArrayList();
            for(ShopcartBO sc: shopcartList){
                ids.add(sc.getSpecId());
            }
            String specIds = StringUtils.join(ids,",");
            shopcartVOS = itemService.queryItemsBySpecIds(specIds);
            for(ShopcartVO vo: shopcartVOS){
                for(ShopcartBO sc: shopcartList){
                    if(sc.getSpecId().equals(vo.getSpecId())){
                        System.out.println("sc.getBuyCounts():"+sc.getBuyCounts());
                        vo.setBuyCounts(sc.getBuyCounts());
                    }
                }
            }

            return UnifyResponse.success(shopcartVOS);
        }else{
            return UnifyResponse.success(new ArrayList<>());
        }

    }

    @ApiOperation(value = "同步购物车到后端",notes = "同步购物车到后端",httpMethod = "POST")
    @PostMapping("sync")
    public UnifyResponse sync(@RequestBody ShopcartBO shopcartBO, HttpServletRequest request){

        String userId = getPassportUserId(request);

        String shopCartJson = redisOperator.get(SHOP_CART + ":" +userId);
        List<ShopcartBO> shopcartList = null;
        if(StringUtils.isNotBlank(shopCartJson)){
            shopcartList = JsonUtils.jsonToList(shopCartJson,ShopcartBO.class);
            boolean isHaving = false;
            for(ShopcartBO sc: shopcartList){
                String temSpecId = sc.getSpecId();
                if(temSpecId.equals(shopcartBO.getSpecId())){
                    sc.setBuyCounts(shopcartBO.getBuyCounts());
                    isHaving = true;
                    break;
                }
            }
            if(!isHaving){
                shopcartList.add(shopcartBO);
            }
        }else{
            shopcartList = new ArrayList<>();
            shopcartList.add(shopcartBO);
        }
        redisOperator.set(SHOP_CART + ":" +userId,JsonUtils.objectToJson(shopcartList));
        return UnifyResponse.success();
    }


    @ApiOperation(value = "删除购物车商品",notes = "删除购物车商品",httpMethod = "GET")
    @GetMapping("del")
    public UnifyResponse del(@RequestParam String itemSpecIds, HttpServletRequest request){

        if(StringUtils.isBlank(itemSpecIds)){
            throw new ParamException(5002);
        }

        String userId = getPassportUserId(request);

        String shopCartJson = redisOperator.get(SHOP_CART + ":" +userId);
        List<ShopcartBO> shopcartList = null;
        List<String> ids = Arrays.asList(itemSpecIds.split(","));
        List<ShopcartBO> toBeRemoveList = new ArrayList<>();
        if(StringUtils.isNotBlank(shopCartJson)){
            shopcartList = JsonUtils.jsonToList(shopCartJson,ShopcartBO.class);
            for(ShopcartBO sc: shopcartList){
                String temSpecId = sc.getSpecId();
                for(String itemSpecId: ids){
                    if(temSpecId.equals(itemSpecId)){
                        toBeRemoveList.add(sc);
                        break;
                    }
                }
            }
            shopcartList.removeAll(toBeRemoveList);
            redisOperator.set(SHOP_CART + ":" +userId,JsonUtils.objectToJson(shopcartList));
        }

        return UnifyResponse.success();
    }
}
